﻿@{
    ViewBag.Title = "Seven服务池";
}
<script src="~/Scripts/jquery.signalR-2.1.2.min.js"></script>
<script src="~/signalr/hubs"></script>
<div class="easyui-panel" data-options="fit:true">
    <div class="easyui-layout" data-options="fit:true">
        <div data-options="region:'north',split:false,title:'服务池状态'" style="height:80px;">
            <span id="msg"></span><span style="color:red;"></span>
            <br /><br />
            <span>Seven用户服务器状态正常！</span>
        </div>
        <div id="connectRecordDiv" data-options="region:'west',split:true,title:'SignalR连接记录',minWidth:450,maxWidth:550" style="width:460px;">
        </div>
        <div data-options="region:'center'">
            <table id="currentDataDg" class="easyui-datagrid" data-options="idField:'ID',fitColumns:true,singleSelect:true,fit:true,border:false,title:'服务池当前状态',toolbar:'#btnForDg'">
                <thead>
                    <tr>
                        <th data-options="width:500,align:'center'" colspan="4">用户登录信息</th>
                        <th data-options="width:500,align:'center'" colspan="2">SignalR连接信息</th>
                    </tr>
                    <tr>
                        <th data-options="field:'Account',width:90,align:'center'">登录名</th>
                        <th data-options="field:'LoginTime',width:120,align:'center'">登录时间</th>
                        <th data-options="field:'LastRequestTime',width:120,align:'center'">上次请求时间</th>
                        <th data-options="field:'OverTime',width:120,align:'center'">距离超时剩余时间</th>
                        <th data-options="field:'SignalRConnectTime',width:90,align:'center'">SignalR连接时间</th>
                        <th data-options="field:'SignalRGroupName',width:90,align:'center'">SignalR分组名</th>
                    </tr>
                </thead>
            </table>
            <div id="btnForDg" class="datagrid-toolbar" style="padding:5px;">
                <a id="btnPush" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">推送信息</a>
                &nbsp;&nbsp;
                <a id="btnReload" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true">刷新</a><span style="color:red;font-weight:bold;"></span>
                &nbsp;&nbsp;
                <a id="btnCheckCount" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true">检查数量</a>
                &nbsp;&nbsp;
                <a id="btnRemoveAll0" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true">清空用户池</a>
                &nbsp;&nbsp;
                <a id="btnRemoveAll" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true">清空SignalR连接</a>
                &nbsp;&nbsp;
                <a id="btnTest" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true">测试webapi</a>
            </div>
        </div>
    </div>
</div>
<div id="pushInfoDia" class="easyui-dialog" data-options="title:'推送信息',iconCls:'icon-save',resizable:true,modal:true,closed:true,cache:false,buttons:'#btnForDia'" style="width:500px;height:250px;padding:15px;">
    目标用户：<span id="target" style="color:red;font-weight:bold;"></span>
    <input type="hidden" id="targetIds" name="targetIds" value="" />
    <br /><br /><br />
    <br /><br /><br />
    推送信息：<textarea id="pushInfo" name="pushInfo" style="width:80%;height:65px;"></textarea>
</div>
<div id="btnForDia">
    <a id="btnSave" class="easyui-linkbutton" data-options="iconCls:'icon-save'">推送</a>
    <a id="btnCancel" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'">关闭</a>
</div>

<script>
    var userName = "@ViewBag.SignalRAdminAccount", groupName = "@ViewBag.SignalRAdminGroup"; var connectID;
    $(function () {
        var chat = $.connection.sevenHub;
        if (typeof chat == "object") { $("#msg").html("SignalR服务状态正常！"); } else { $("#msg").html("SignalR服务状态不正常！"); }

        //SignalR注册成功时调用
        chat.client.getWelcomeInfo = function (message, connectedId) {
            $("#msg").next().html(userName + message + "您的连接ID为：" + connectedId);
        }

        //显示连接记录
        chat.client.getActionRecord = function (info) {
            $("#connectRecordDiv").append(info + "<br />");
            $("#btnReload").click();
        };

        //显示公共信息
        chat.client.getPublicInfo = function (sender, info) {
            //$("#pushInfo").append(info + "<br />");
        };

        //显示推送信息
        chat.client.getPushInfo = function (sender, info) {
            alert(info);
        };

        //刷新服务池数据
        chat.client.reloadServiceData = function () {
            $("#btnReload").click();
        };

        chat.client.stopClient = function () {
            chat.connection.stop();
        }

        $.connection.hub.start().done(function () {
            connectID = $.connection.hub.id;
            chat.server.registeUserConnect(userName, "", groupName);

            //更新当前数据状态
            $("#btnReload").click(function () {
                $.post("/Home/GetCurrentData", function (result) {
                    $("#btnReload").next().text("最近刷新时间为：" + new Date().format("yyyy-MM-dd HH:mm:ss"));
                    $("#currentDataDg").datagrid("loadData", result);
                });
            });

            //推送信息窗口
            $("#btnPush").click(function () {
                var rows = $("#currentDataDg").datagrid("getSelections");
                if (rows.length) {
                    $("#target", "#pushInfoDia").text($.array.map(rows, function (item) { return item.Account; }).join(","));
                    $("#targetIds", "#pushInfoDia").val($.array.map(rows, function (item) { return item.SignalRID; }).join(","));
                    $("#pushInfoDia").dialog("open");
                }
            });

            //dialog按钮 执行信息推送
            $("#btnSave", "#btnForDia").click(function () {
                var pushMsg = $("#pushInfo", "#pushInfoDia").val();
                if (!$.string.isNullOrWhiteSpace(pushMsg)) {
                    var callback = function (cid, uname, gname) {
                        $("#btnCancel", "#btnForDia").click();
                        chat.server.pushInfoFromJs(uname, $("#targetIds", "#pushInfoDia").val(), pushMsg);
                    };
                    execAfterStart(callback, connectID, userName, groupName);
                }
            });
            //dialog按钮 关闭
            $("#btnCancel", "#btnForDia").click(function () {
                $("#pushInfoDia").dialog("close");
                $("#target", "#pushInfoDia").text("");
                $("#pushInfo", "#pushInfoDia").val("");
            });

            $("#btnRemoveAll").click(function () {
                var callback = function (cid, uname, gname) {
                    chat.server.removeAllConnect(true);
                };

                execAfterStart(callback);
            });
        });

        //callback的参数次序必须是cid, uname, gname，无论是否可以用到这些参数
        var execAfterStart = function (callback, cid, uname, gname) {
            //判定是否连接
            if ($.connection.hub.state == $.signalR.connectionState.disconnected) {
                $.connection.hub.start().done(function () {
                    connectID = $.connection.hub.id; cid = connectID;
                    chat.server.registeUserConnect(uname, gname);
                    if ($.isFunction(callback)) { callback.call(this, cid, uname, gname); }
                });
            }
            else {
                if ($.isFunction(callback)) { callback.call(this, cid, uname, gname); }
            }
        };

        //清空用户池
        $("#btnRemoveAll0").click(function () {
            $.post("/Home/ClearMemberPool", function (result) { });
        });

        //检查服务池中连接数和用户数
        $("#btnCheckCount").click(function () {
            $.post("/Home/GetPoolCount", function (result) {
                alert("在线用户数量：" + result.userCount + "，SignalR连接数量：" + result.signalRCount + "，今日被调用次数：" + result.todayCount);
            });
        });

        //webapi访问测试
        $("#btnTest").click(function () {

            //$.ajax({
            //    url: "api/MemberService/",
            //    type: "post",
            //    data: { Account: "cat", Password: "1" },
            //    success: function (data) {
            //        alert(data);
            //    }
            //});

            $.ajax({
                url: "/api/MemberService/NormalLogin",
                type: "post",
                data: JSON.stringify({ Account: "cat", Password: "1" }),
                contentType: "application/json",
                success: function (data) {
                    alert(data.Success);
                }
            });

            //$.ajax({
            //    url: "/api/MemberService/Access",
            //    type: "get",
            //    success: function (result) {
            //        alert(result);
            //    }
            //});

        });
    });
</script>

